<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){

    	// if(isset($_SESSION['uid'])){
    	// 	 $this->error('已经登录',U('home/set/index'));
    	// }

    	$question = D('vercode')->getOne();

    	$this->assign('question',$question);
        $this->assign('title',"登录");
        $this->display();
    }
    //登录检测
    public function checkdata()
    {
    	$data = I('post.');

    	$res = D('vercode')->checkCode($data['vercode']);

    	if(!$res){
    		$this->ajaxReturn(['error'=>1,'info'=>"人类验证不通过"]);
    	}

    	$email = $data['email'];
    	$password = md5($data['password']);

    	$user = M('user')->where(['email'=>$email,'password'=>$password])->find();


    	if($user){
            if($user['status']==1){
                $this->ajaxReturn(['error'=>1,'info'=>"帐号被封，请联系110开通"]);
            }

    		$_SESSION['uid']= $user['uid'];
    		$_SESSION['nickname']= $user['nickname'];
    		$_SESSION['face']= $user['face'];
    		$this->ajaxReturn(['error'=>0,'info'=>"登录成功"]);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>"邮箱或者密码不正确"]);
    	}

    }
    //退出
    public function logout()
    {
    	session_unset();
    	session_destroy();
    	$this->success('退出成功', U('home/login/index'));
    }


    public function qqlogin(){
        Vendor('qqConnect.Api.qqConnectAPI');
        // require_once("../../API/qqConnectAPI.php");
        $qc = new \QC();
        $qc->qq_login();
    }

    public function qqreturn()
    {
        Vendor('qqConnect.Api.qqConnectAPI');
        $qc = new \QC();
        $acs = $qc->qq_callback();
        $openid =  $qc->get_openid();


        $user = M('user')->where(['openid'=>$openid])->find();

        if($user){
            $_SESSION['uid']= $user['uid'];
            $_SESSION['nickname']= $user['nickname'];
            $_SESSION['face']= $user['face'];
            $this->success("登录成功",U('home/index/index'));
        }else{

            $qc = new \QC($acs,$openid);
            $user = $qc->get_user_info();


            $data['nickname'] = $user['nickname'];
            $data['sex'] = $user['gender'];
            $data['city'] = $user['city'];
            $data['create_time'] =time();
            $data['openid'] = $openid;
            $data['face'] = $this->getqqface($user['figureurl_2']);

            $uid = M('user')->add($data);

            if($uid){

                $_SESSION['uid']= $uid;
                $_SESSION['nickname']= $data['nickname'];
                $_SESSION['face']= $data['face'];
                $this->success("登录成功",U('home/index/index'));
            }
        }

        
    }

    public function getqqface($url)
    {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        $output = curl_exec($ch);

        $path = "./Uploads/qq/".uniqid().".jpg";

        file_put_contents($path, $output);

        curl_close($ch);

        return $path;

    }


    public function wxlogin()
    {
        //引导用户来访问这个方法 
        //
        // 获取code
        // 
        $appid = "wxbac82045a637c02e";
        $redirect_uri = urlencode("http://1o533n8683.imwork.net/P201703/bbs/index.php/home/login/wxreturn");

        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect";


        header("Location:$url");

    }

    public function wxreturn()
    {
        $appid = "wxbac82045a637c02e";
        $secret = "7c320fea54e77a01fcc59ff7060b04e2";
        $code = $_GET['code'];

        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code";

        $data = file_get_contents($url);

        $data = json_decode($data,true);

        $openid = $data['openid'];


        $user = M('user')->where(['wxopenid'=>$openid])->find();

        if($user){
            $_SESSION['uid']= $user['uid'];
            $_SESSION['nickname']= $user['nickname'];
            $_SESSION['face']= $user['face'];
            $this->success("登录成功",U('home/index/index'));
        }else{

            $access_token = $data['access_token'];


            $url ="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";


            $userinfo = file_get_contents($url);

            $userinfo =  json_decode($userinfo,true);

            var_dump($userinfo);


            $data['nickname']    = $userinfo['nickname'];
            $data['sex']         = $userinfo['sex']==1?'男':"女";
            $data['city']        = $userinfo['city'];
            $data['create_time'] =time();
            $data['wxopenid']      = $openid;
            $data['face']        = $this->getqqface($userinfo['headimgurl']);

            $uid = M('user')->add($data);

            if($uid){

                $_SESSION['uid']= $uid;
                $_SESSION['nickname']= $data['nickname'];
                $_SESSION['face']= $data['face'];
                $this->success("登录成功",U('home/index/index'));
            }
        }






    }
}